home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / variable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-27  |  27.2 KB  |  1,021 lines

  1. /* Internals of variables for GNU Make.
  2. Copyright (C) 1988,89,90,91,92,93,94,96,97 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21. #include "filedef.h"
  22. #include "job.h"
  23. #include "commands.h"
  24. #include "variable.h"
  25. #ifdef WINDOWS32
  26. #include "pathstuff.h"
  27. #endif
  28.  
  29. /* Hash table of all global variable definitions.  */
  30.  
  31. #ifndef    VARIABLE_BUCKETS
  32. #define VARIABLE_BUCKETS        523
  33. #endif
  34. #ifndef    PERFILE_VARIABLE_BUCKETS
  35. #define    PERFILE_VARIABLE_BUCKETS    23
  36. #endif
  37. #ifndef    SMALL_SCOPE_VARIABLE_BUCKETS
  38. #define    SMALL_SCOPE_VARIABLE_BUCKETS    13
  39. #endif
  40. static struct variable *variable_table[VARIABLE_BUCKETS];
  41. static struct variable_set global_variable_set
  42.   = { variable_table, VARIABLE_BUCKETS };
  43. static struct variable_set_list global_setlist
  44.   = { 0, &global_variable_set };
  45. struct variable_set_list *current_variable_set_list = &global_setlist;
  46.  
  47. static struct variable *define_variable_in_set PARAMS ((char *name, unsigned int length,
  48.                             char *value, enum variable_origin origin,
  49.                             int recursive, struct variable_set *set));
  50.  
  51.  
  52. /* Implement variables.  */
  53.  
  54. /* Define variable named NAME with value VALUE in SET.  VALUE is copied.
  55.    LENGTH is the length of NAME, which does not need to be null-terminated.
  56.    ORIGIN specifies the origin of the variable (makefile, command line
  57.    or environment).
  58.    If RECURSIVE is nonzero a flag is set in the variable saying
  59.    that it should be recursively re-expanded.  */
  60.  
  61. static struct variable *
  62. define_variable_in_set (name, length, value, origin, recursive, set)
  63.      char *name;
  64.      unsigned int length;
  65.      char *value;
  66.      enum variable_origin origin;
  67.      int recursive;
  68.      struct variable_set *set;
  69. {
  70.   register unsigned int i;
  71.   register unsigned int hashval;
  72.   register struct variable *v;
  73.  
  74.   hashval = 0;
  75.   for (i = 0; i < length; ++i)
  76.     HASH (hashval, name[i]);
  77.   hashval %= set->buckets;
  78.  
  79.   for (v = set->table[hashval]; v != 0; v = v->next)
  80.     if (*v->name == *name
  81.     && !strncmp (v->name + 1, name + 1, length - 1)
  82.     && v->name[length] == '\0')
  83.       break;
  84.  
  85.   if (env_overrides && origin == o_env)
  86.     origin = o_env_override;
  87.  
  88.   if (v != 0)
  89.     {
  90.       if (env_overrides && v->origin == o_env)
  91.     /* V came from in the environment.  Since it was defined
  92.        before the switches were parsed, it wasn't affected by -e.  */
  93.     v->origin = o_env_override;
  94.  
  95.       /* A variable of this name is already defined.
  96.      If the old definition is from a stronger source
  97.      than this one, don't redefine it.  */
  98.       if ((int) origin >= (int) v->origin)
  99.     {
  100.       if (v->value != 0)
  101.         free (v->value);
  102.       v->value = savestring (value, strlen (value));
  103.       v->origin = origin;
  104.       v->recursive = recursive;
  105.     }
  106.       return v;
  107.     }
  108.  
  109.   /* Create a new variable definition and add it to the hash table.  */
  110.  
  111.   v = (struct variable *) xmalloc (sizeof (struct variable));
  112.   v->name = savestring (name, length);
  113.   v->value = savestring (value, strlen (value));
  114.   v->origin = origin;
  115.   v->recursive = recursive;
  116.   v->expanding = 0;
  117.   v->export = v_default;
  118.   v->next = set->table[hashval];
  119.   set->table[hashval] = v;
  120.   return v;
  121. }
  122.  
  123. /* Define a variable in the current variable set.  */
  124.  
  125. struct variable *
  126. define_variable (name, length, value, origin, recursive)
  127.      char *name;
  128.      unsigned int length;
  129.      char *value;
  130.      enum variable_origin origin;
  131.      int recursive;
  132. {
  133.   return define_variable_in_set (name, length, value, origin, recursive,
  134.                  current_variable_set_list->set);
  135. }
  136.  
  137. /* Define a variable in FILE's variable set.  */
  138.  
  139. struct variable *
  140. define_variable_for_file (name, length, value, origin, recursive, file)
  141.      char *name;
  142.      unsigned int length;
  143.      char *value;
  144.      enum variable_origin origin;
  145.      int recursive;
  146.      struct file *file;
  147. {
  148.   return define_variable_in_set (name, length, value, origin, recursive,
  149.                  file->variables->set);
  150. }
  151.  
  152. /* Lookup a variable whose name is a string starting at NAME
  153.    and with LENGTH chars.  NAME need not be null-terminated.
  154.    Returns address of the `struct variable' containing all info
  155.    on the variable, or nil if no such variable is defined.  */
  156.  
  157. struct variable *
  158. lookup_variable (name, length)
  159.      char *name;
  160.      unsigned int length;
  161. {
  162.   register struct variable_set_list *setlist;
  163.  
  164.   register unsigned int i;
  165.   register unsigned int rawhash = 0;
  166.  
  167.   for (i = 0; i < length; ++i)
  168.     HASH (rawhash, name[i]);
  169.  
  170.   for (setlist = current_variable_set_list;
  171.        setlist != 0; setlist = setlist->next)
  172.     {
  173.       register struct variable_set *set = setlist->set;
  174.       register unsigned int hashval = rawhash % set->buckets;
  175.       register struct variable *v;
  176.  
  177.       for (v = set->table[hashval]; v != 0; v = v->next)
  178.     if (*v->name == *name
  179.         && !strncmp (v->name + 1, name + 1, length - 1)
  180.         && v->name[length] == 0)
  181.       return v;
  182.     }
  183.  
  184.   return 0;
  185. }
  186.  
  187. /* Initialize FILE's variable set list.  If FILE already has a variable set
  188.    list, the topmost variable set is left intact, but the the rest of the
  189.    chain is replaced with FILE->parent's setlist.  */
  190.  
  191. void
  192. initialize_file_variables (file)
  193.      struct file *file;
  194. {
  195.   register struct variable_set_list *l = file->variables;
  196.   if (l == 0)
  197.     {
  198.       l = (struct variable_set_list *)
  199.     xmalloc (sizeof (struct variable_set_list));
  200.       l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  201.       l->set->buckets = PERFILE_VARIABLE_BUCKETS;
  202.       l->set->table = (struct variable **)
  203.     xmalloc (l->set->buckets * sizeof (struct variable *));
  204.       bzero ((char *) l->set->table,
  205.          l->set->buckets * sizeof (struct variable *));
  206.       file->variables = l;
  207.     }
  208.  
  209.   if (file->parent == 0)
  210.     l->next = &global_setlist;
  211.   else
  212.     {
  213.       if (file->parent->variables == 0)
  214.     initialize_file_variables (file->parent);
  215.       l->next = file->parent->variables;
  216.     }
  217. }
  218.  
  219. /* Pop the top set off the current variable set list,
  220.    and free all its storage.  */
  221.  
  222. void
  223. pop_variable_scope ()
  224. {
  225.   register struct variable_set_list *setlist = current_variable_set_list;
  226.   register struct variable_set *set = setlist->set;
  227.   register unsigned int i;
  228.  
  229.   current_variable_set_list = setlist->next;
  230.   free ((char *) setlist);
  231.  
  232.   for (i = 0; i < set->buckets; ++i)
  233.     {
  234.       register struct variable *next = set->table[i];
  235.       while (next != 0)
  236.     {
  237.       register struct variable *v = next;
  238.       next = v->next;
  239.  
  240.       free (v->name);
  241.       free ((char *) v);
  242.     }
  243.     }
  244.   free ((char *) set->table);
  245.   free ((char *) set);
  246. }
  247.  
  248. /* Create a new variable set and push it on the current setlist.  */
  249.  
  250. void
  251. push_new_variable_scope ()
  252. {
  253.   register struct variable_set_list *setlist;
  254.   register struct variable_set *set;
  255.  
  256.   set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  257.   set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
  258.   set->table = (struct variable **)
  259.     xmalloc (set->buckets * sizeof (struct variable *));
  260.   bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
  261.  
  262.   setlist = (struct variable_set_list *)
  263.     xmalloc (sizeof (struct variable_set_list));
  264.   setlist->set = set;
  265.   setlist->next = current_variable_set_list;
  266.   current_variable_set_list = setlist;
  267. }
  268.  
  269. /* Merge SET1 into SET0, freeing unused storage in SET1.  */
  270.  
  271. static void
  272. merge_variable_sets (set0, set1)
  273.      struct variable_set *set0, *set1;
  274. {
  275.   register unsigned int bucket1;
  276.  
  277.   for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
  278.     {
  279.       register struct variable *v1 = set1->table[bucket1];
  280.       while (v1 != 0)
  281.     {
  282.       struct variable *next = v1->next;
  283.       unsigned int bucket0;
  284.       register struct variable *v0;
  285.  
  286.       if (set1->buckets >= set0->buckets)
  287.         bucket0 = bucket1;
  288.       else
  289.         {
  290.           register char *n;
  291.           bucket0 = 0;
  292.           for (n = v1->name; *n != '\0'; ++n)
  293.         HASH (bucket0, *n);
  294.         }
  295.       bucket0 %= set0->buckets;
  296.  
  297.       for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
  298.         if (streq (v0->name, v1->name))
  299.           break;
  300.  
  301.       if (v0 == 0)
  302.         {
  303.           /* There is no variable in SET0 with the same name.  */
  304.           v1->next = set0->table[bucket0];
  305.           set0->table[bucket0] = v1;
  306.         }
  307.       else
  308.         {
  309.           /* The same variable exists in both sets.
  310.          SET0 takes precedence.  */
  311.           free (v1->value);
  312.           free ((char *) v1);
  313.         }
  314.  
  315.       v1 = next;
  316.     }
  317.     }
  318. }
  319.  
  320. /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
  321.  
  322. void
  323. merge_variable_set_lists (setlist0, setlist1)
  324.      struct variable_set_list **setlist0, *setlist1;
  325. {
  326.   register struct variable_set_list *list0 = *setlist0;
  327.   struct variable_set_list *last0 = 0;
  328.  
  329.   while (setlist1 != 0 && list0 != 0)
  330.     {
  331.       struct variable_set_list *next = setlist1;
  332.       setlist1 = setlist1->next;
  333.  
  334.       merge_variable_sets (list0->set, next->set);
  335.  
  336.       last0 = list0;
  337.       list0 = list0->next;
  338.     }
  339.  
  340.   if (setlist1 != 0)
  341.     {
  342.       if (last0 == 0)
  343.     *setlist0 = setlist1;
  344.       else
  345.     last0->next = setlist1;
  346.     }
  347. }
  348.  
  349. /* Define the automatic variables, and record the addresses
  350.    of their structures so we can change their values quickly.  */
  351.  
  352. void
  353. define_automatic_variables ()
  354. {
  355. #ifdef WINDOWS32
  356.   extern char* default_shell;
  357. #else
  358.   extern char default_shell[];
  359. #endif
  360.   register struct variable *v;
  361.   char buf[200];
  362.  
  363.   sprintf (buf, "%u", makelevel);
  364.   (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
  365.  
  366.   sprintf (buf, "%s%s%s",
  367.        version_string,
  368.        (remote_description == 0 || remote_description[0] == '\0')
  369.        ? "" : "-",
  370.        (remote_description == 0 || remote_description[0] == '\0')
  371.        ? "" : remote_description);
  372.   (void) define_variable ("MAKE_VERSION", 12, buf, o_default, 0);
  373.  
  374. #ifdef  __MSDOS__
  375.   /* Allow to specify a special shell just for Make,
  376.      and use $COMSPEC as the default $SHELL when appropriate.  */
  377.   {
  378.     static char shell_str[] = "SHELL";
  379.     const int shlen = sizeof (shell_str) - 1;
  380.     struct variable *mshp = lookup_variable ("MAKESHELL", 9);
  381.     struct variable *comp = lookup_variable ("COMSPEC", 7);
  382.  
  383.     /* Make $MAKESHELL override $SHELL even if -e is in effect.  */
  384.     if (mshp)
  385.       (void) define_variable (shell_str, shlen,
  386.                   mshp->value, o_env_override, 0);
  387.     else if (comp)
  388.       {
  389.     /* $COMSPEC shouldn't override $SHELL.  */
  390.     struct variable *shp = lookup_variable (shell_str, shlen);
  391.  
  392.     if (!shp)
  393.       (void) define_variable (shell_str, shlen, comp->value, o_env, 0);
  394.       }
  395.   }
  396. #endif
  397.  
  398.   /* This won't override any definition, but it
  399.      will provide one if there isn't one there.  */
  400.   v = define_variable ("SHELL", 5, default_shell, o_default, 0);
  401.   v->export = v_export;        /* Always export SHELL.  */
  402.  
  403.   /* On MSDOS we do use SHELL from environment, since
  404.      it isn't a standard environment variable on MSDOS,
  405.      so whoever sets it, does that on purpose.  */
  406. #ifndef __MSDOS__
  407.   /* Don't let SHELL come from the environment.  */
  408.   if (*v->value == '\0' || v->origin == o_env || v->origin == o_env_override)
  409.     {
  410.       free (v->value);
  411.       v->origin = o_file;
  412.       v->value = savestring (default_shell, strlen (default_shell));
  413.     }
  414. #endif
  415.  
  416.   /* Make sure MAKEFILES gets exported if it is set.  */
  417.   v = define_variable ("MAKEFILES", 9, "", o_default, 0);
  418.   v->export = v_ifset;
  419.  
  420.   /* Define the magic D and F variables in terms of
  421.      the automatic variables they are variations of.  */
  422.  
  423.   define_variable ("@D", 2, "$(patsubst %/,%,$(dir $@))", o_automatic, 1);
  424.   define_variable ("%D", 2, "$(patsubst %/,%,$(dir $%))", o_automatic, 1);
  425.   define_variable ("*D", 2, "$(patsubst %/,%,$(dir $*))", o_automatic, 1);
  426.   define_variable ("<D", 2, "$(patsubst %/,%,$(dir $<))", o_automatic, 1);
  427.   define_variable ("?D", 2, "$(patsubst %/,%,$(dir $?))", o_automatic, 1);
  428.   define_variable ("^D", 2, "$(patsubst %/,%,$(dir $^))", o_automatic, 1);
  429.   define_variable ("+D", 2, "$(patsubst %/,%,$(dir $+))", o_automatic, 1);
  430.   define_variable ("@F", 2, "$(notdir $@)", o_automatic, 1);
  431.   define_variable ("%F", 2, "$(notdir $%)", o_automatic, 1);
  432.   define_variable ("*F", 2, "$(notdir $*)", o_automatic, 1);
  433.   define_variable ("<F", 2, "$(notdir $<)", o_automatic, 1);
  434.   define_variable ("?F", 2, "$(notdir $?)", o_automatic, 1);
  435.   define_variable ("^F", 2, "$(notdir $^)", o_automatic, 1);
  436.   define_variable ("+F", 2, "$(notdir $+)", o_automatic, 1);
  437. }
  438.  
  439. int export_all_variables;
  440.  
  441. /* Create a new environment for FILE's commands.
  442.    If FILE is nil, this is for the `shell' function.
  443.    The child's MAKELEVEL variable is incremented.  */
  444.  
  445. char **
  446. target_environment (file)
  447.      struct file *file;
  448. {
  449.   struct variable_set_list *set_list;
  450.   register struct variable_set_list *s;
  451.   struct variable_bucket
  452.     {
  453.       struct variable_bucket *next;
  454.       struct variable *variable;
  455.     };
  456.   struct variable_bucket **table;
  457.   unsigned int buckets;
  458.   register unsigned int i;
  459.   register unsigned nvariables;
  460.   char **result;
  461.   unsigned int mklev_hash;
  462.  
  463.   if (file == 0)
  464.     set_list = current_variable_set_list;
  465.   else
  466.     set_list = file->variables;
  467.  
  468.   /* Find the lowest number of buckets in any set in the list.  */
  469.   s = set_list;
  470.   buckets = s->set->buckets;
  471.   for (s = s->next; s != 0; s = s->next)
  472.     if (s->set->buckets < buckets)
  473.       buckets = s->set->buckets;
  474.  
  475.   /* Find the hash value of the bucket `MAKELEVEL' will fall into.  */
  476.   {
  477.     char *p = "MAKELEVEL";
  478.     mklev_hash = 0;
  479.     while (*p != '\0')
  480.       HASH (mklev_hash, *p++);
  481.   }
  482.  
  483.   /* Temporarily allocate a table with that many buckets.  */
  484.   table = (struct variable_bucket **)
  485.     alloca (buckets * sizeof (struct variable_bucket *));
  486.   bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
  487.  
  488.   /* Run through all the variable sets in the list,
  489.      accumulating variables in TABLE.  */
  490.   nvariables = 0;
  491.   for (s = set_list; s != 0; s = s->next)
  492.     {
  493.       register struct variable_set *set = s->set;
  494.       for (i = 0; i < set->buckets; ++i)
  495.     {
  496.       register struct variable *v;
  497.       for (v = set->table[i]; v != 0; v = v->next)
  498.         {
  499.           unsigned int j = i % buckets;
  500.           register struct variable_bucket *ov;
  501.           register char *p = v->name;
  502.  
  503.           if (i == mklev_hash % set->buckets
  504.           && streq (v->name, "MAKELEVEL"))
  505.         /* Don't include MAKELEVEL because it will be
  506.            added specially at the end.  */
  507.         continue;
  508.  
  509.           switch (v->export)
  510.         {
  511.         case v_default:
  512.           if (v->origin == o_default || v->origin == o_automatic)
  513.             /* Only export default variables by explicit request.  */
  514.             continue;
  515.  
  516.           if (! export_all_variables
  517.               && v->origin != o_command
  518.               && v->origin != o_env && v->origin != o_env_override)
  519.             continue;
  520.  
  521.           if (*p != '_' && (*p < 'A' || *p > 'Z')
  522.               && (*p < 'a' || *p > 'z'))
  523.             continue;
  524.           for (++p; *p != '\0'; ++p)
  525.             if (*p != '_' && (*p < 'a' || *p > 'z')
  526.             && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
  527.               break;
  528.           if (*p != '\0')
  529.             continue;
  530.  
  531.         case v_export:
  532.           break;
  533.  
  534.         case v_noexport:
  535.           continue;
  536.  
  537.         case v_ifset:
  538.           if (v->origin == o_default)
  539.             continue;
  540.           break;
  541.         }
  542.  
  543.           for (ov = table[j]; ov != 0; ov = ov->next)
  544.         if (streq (v->name, ov->variable->name))
  545.           break;
  546.           if (ov == 0)
  547.         {
  548.           register struct variable_bucket *entry;
  549.           entry = (struct variable_bucket *)
  550.             alloca (sizeof (struct variable_bucket));
  551.           entry->next = table[j];
  552.           entry->variable = v;
  553.           table[j] = entry;
  554.           ++nvariables;
  555.         }
  556.         }
  557.     }
  558.     }
  559.  
  560.   result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
  561.   nvariables = 0;
  562.   for (i = 0; i < buckets; ++i)
  563.     {
  564.       register struct variable_bucket *b;
  565.       for (b = table[i]; b != 0; b = b->next)
  566.     {
  567.       register struct variable *v = b->variable;
  568.       /* If V is recursively expanded and didn't come from the environment,
  569.          expand its value.  If it came from the environment, it should
  570.          go back into the environment unchanged.  */
  571.       if (v->recursive
  572.           && v->origin != o_env && v->origin != o_env_override)
  573.         {
  574.           char *value = recursively_expand (v);
  575. #ifdef WINDOWS32
  576.               if (strcmp(v->name, "Path") == 0 ||
  577.                   strcmp(v->name, "PATH") == 0)
  578.                 convert_Path_to_windows32(value, ';');
  579. #endif
  580.           result[nvariables++] = concat (v->name, "=", value);
  581.           free (value);
  582.         }
  583.       else
  584. #ifdef WINDOWS32
  585.           {
  586.             if (strcmp(v->name, "Path") == 0 ||
  587.                 strcmp(v->name, "PATH") == 0)
  588.               convert_Path_to_windows32(v->value, ';');
  589.             result[nvariables++] = concat (v->name, "=", v->value);
  590.           }
  591. #else
  592.         result[nvariables++] = concat (v->name, "=", v->value);
  593. #endif
  594.     }
  595.     }
  596.   result[nvariables] = (char *) xmalloc (100);
  597.   (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
  598.   result[++nvariables] = 0;
  599.  
  600.   return result;
  601. }
  602.  
  603. /* Try to interpret LINE (a null-terminated string) as a variable definition.
  604.  
  605.    ORIGIN may be o_file, o_override, o_env, o_env_override,
  606.    or o_command specifying that the variable definition comes
  607.    from a makefile, an override directive, the environment with
  608.    or without the -e switch, or the command line.
  609.  
  610.    A variable definition has the form "name = value" or "name := value".
  611.    Any whitespace around the "=" or ":=" is removed.  The first form
  612.    defines a variable that is recursively re-evaluated.  The second form
  613.    defines a variable whose value is variable-expanded at the time of
  614.    definition and then is evaluated only once at the time of expansion.
  615.  
  616.    If a variable was defined, a pointer to its `struct variable' is returned.
  617.    If not, NULL is returned.  */
  618.  
  619. struct variable *
  620. try_variable_definition (filename, lineno, line, origin)
  621.      char *filename;
  622.      unsigned int lineno;
  623.      char *line;
  624.      enum variable_origin origin;
  625. {
  626.   register int c;
  627.   register char *p = line;
  628.   register char *beg;
  629.   register char *end;
  630.   enum { bogus, simple, recursive, append } flavor = bogus;
  631.   char *name, *expanded_name, *value;
  632.   struct variable *v;
  633.  
  634.   while (1)
  635.     {
  636.       c = *p++;
  637.       if (c == '\0' || c == '#')
  638.     return 0;
  639.       if (c == '=')
  640.     {
  641.       end = p - 1;
  642.       flavor = recursive;
  643.       break;
  644.     }
  645.       else if (c == ':')
  646.     if (*p == '=')
  647.       {
  648.         end = p++ - 1;
  649.         flavor = simple;
  650.         break;
  651.       }
  652.     else
  653.       /* A colon other than := is a rule line, not a variable defn.  */
  654.       return 0;
  655.       else if (c == '+' && *p == '=')
  656.     {
  657.       end = p++ - 1;
  658.       flavor = append;
  659.       break;
  660.     }
  661.       else if (c == '$')
  662.     {
  663.       /* This might begin a variable expansion reference.  Make sure we
  664.          don't misrecognize chars inside the reference as =, := or +=.  */
  665.       char closeparen;
  666.       int count;
  667.       c = *p++;
  668.       if (c == '(')
  669.         closeparen = ')';
  670.       else if (c == '{')
  671.         closeparen = '}';
  672.       else
  673.         continue;        /* Nope.  */
  674.  
  675.       /* P now points past the opening paren or brace.
  676.          Count parens or braces until it is matched.  */
  677.       count = 0;
  678.       for (; *p != '\0'; ++p)
  679.         {
  680.           if (*p == c)
  681.         ++count;
  682.           else if (*p == closeparen && --count < 0)
  683.         {
  684.           ++p;
  685.           break;
  686.         }
  687.         }
  688.     }
  689.     }
  690.  
  691.   beg = next_token (line);
  692.   while (end > beg && isblank (end[-1]))
  693.     --end;
  694.   p = next_token (p);
  695.  
  696.   /* Expand the name, so "$(foo)bar = baz" works.  */
  697.   name = (char *) alloca (end - beg + 1);
  698.   bcopy (beg, name, end - beg);
  699.   name[end - beg] = '\0';
  700.   expanded_name = allocated_variable_expand (name);
  701.  
  702.   if (expanded_name[0] == '\0')
  703.     {
  704.       if (filename == 0)
  705.     fatal ("empty variable name");
  706.       else
  707.     makefile_fatal (filename, lineno, "empty variable name");
  708.     }
  709.  
  710.   /* Calculate the variable's new value in VALUE.  */
  711.  
  712.   switch (flavor)
  713.     {
  714.     case bogus:
  715.       /* Should not be possible.  */
  716.       abort ();
  717.       return 0;
  718.     case simple:
  719.       /* A simple variable definition "var := value".  Expand the value.  */
  720.       value = variable_expand (p);
  721.       break;
  722.     case recursive:
  723.       /* A recursive variable definition "var = value".
  724.      The value is used verbatim.  */
  725.       value = p;
  726.       break;
  727.     case append:
  728.       /* An appending variable definition "var += value".
  729.      Extract the old value and append the new one.  */
  730.       v = lookup_variable (expanded_name, strlen (expanded_name));
  731.       if (v == 0)
  732.     {
  733.       /* There was no old value.
  734.          This becomes a normal recursive definition.  */
  735.       value = p;
  736.       flavor = recursive;
  737.     }
  738.       else
  739.     {
  740.       /* Paste the old and new values together in VALUE.  */
  741.  
  742.       unsigned int oldlen, newlen;
  743.  
  744.       if (v->recursive)
  745.         /* The previous definition of the variable was recursive.
  746.            The new value comes from the unexpanded old and new values.  */
  747.         flavor = recursive;
  748.       else
  749.         /* The previous definition of the variable was simple.
  750.            The new value comes from the old value, which was expanded
  751.            when it was set; and from the expanded new value.  */
  752.         p = variable_expand (p);
  753.  
  754.       oldlen = strlen (v->value);
  755.       newlen = strlen (p);
  756.       value = (char *) alloca (oldlen + 1 + newlen + 1);
  757.       bcopy (v->value, value, oldlen);
  758.       value[oldlen] = ' ';
  759.       bcopy (p, &value[oldlen + 1], newlen + 1);
  760.     }
  761.     }
  762.  
  763. #ifdef __MSDOS__
  764.   /* Many Unix Makefiles include a line saying "SHELL=/bin/sh", but
  765.      non-Unix systems don't conform to this default configuration (in
  766.      fact, most of them don't even have `/bin').  On the other hand,
  767.      $SHELL in the environment, if set, points to the real pathname of
  768.      the shell.
  769.      Therefore, we generally won't let lines like "SHELL=/bin/sh" from
  770.      the Makefile override $SHELL from the environment.  But first, we
  771.      look for the basename of the shell in the directory where SHELL=
  772.      points, and along the $PATH; if it is found in any of these places,
  773.      we define $SHELL to be the actual pathname of the shell.  Thus, if
  774.      you have bash.exe installed as d:/unix/bash.exe, and d:/unix is on
  775.      your $PATH, then SHELL=/usr/local/bin/bash will have the effect of
  776.      defining SHELL to be "d:/unix/bash.exe".  */
  777.   if (origin == o_file
  778.       && strcmp (expanded_name, "SHELL") == 0)
  779.     {
  780.       char shellpath[PATH_MAX];
  781.       extern char * __dosexec_find_on_path (const char *, char *[], char *);
  782.  
  783.       /* See if we can find "/bin/sh.exe", "/bin/sh.com", etc.  */
  784.       if (__dosexec_find_on_path (value, (char **)0, shellpath))
  785.     {
  786.       char *p;
  787.  
  788.       for (p = shellpath; *p; p++)
  789.         {
  790.           if (*p == '\\')
  791.         *p = '/';
  792.         }
  793.       v = define_variable (expanded_name, strlen (expanded_name),
  794.                    shellpath, origin, flavor == recursive);
  795.     }
  796.       else
  797.     {
  798.       char *shellbase, *bslash;
  799.       struct variable *pathv = lookup_variable ("PATH", 4);
  800.       char *path_string;
  801.       char *fake_env[2];
  802.       size_t pathlen = 0;
  803.  
  804.       shellbase = rindex (value, '/');
  805.       bslash = rindex (value, '\\');
  806.       if (!shellbase || bslash > shellbase)
  807.         shellbase = bslash;
  808.       if (!shellbase && value[1] == ':')
  809.         shellbase = value + 1;
  810.       if (shellbase)
  811.         shellbase++;
  812.       else
  813.         shellbase = value;
  814.  
  815.       /* Search for the basename of the shell (with standard
  816.          executable extensions) along the $PATH.  */
  817.       if (pathv)
  818.         pathlen = strlen (pathv->value);
  819.       path_string = (char *)xmalloc (5 + pathlen + 2 + 1);
  820.       /* On MSDOS, current directory is considered as part of $PATH.  */
  821.       sprintf (path_string, "PATH=.;%s", pathv ? pathv->value : "");
  822.       fake_env[0] = path_string;
  823.       fake_env[1] = (char *)0;
  824.       if (__dosexec_find_on_path (shellbase, fake_env, shellpath))
  825.         {
  826.           char *p;
  827.  
  828.           for (p = shellpath; *p; p++)
  829.         {
  830.           if (*p == '\\')
  831.             *p = '/';
  832.         }
  833.           v = define_variable (expanded_name, strlen (expanded_name),
  834.                    shellpath, origin, flavor == recursive);
  835.         }
  836.       else
  837.         v = lookup_variable (expanded_name, strlen (expanded_name));
  838.  
  839.       free (path_string);
  840.     }
  841.     }
  842.   else
  843. #endif /* __MSDOS__ */
  844.  
  845.   v = define_variable (expanded_name, strlen (expanded_name),
  846.                value, origin, flavor == recursive);
  847.  
  848.   free (expanded_name);
  849.  
  850.   return v;
  851. }
  852.  
  853. /* Print information for variable V, prefixing it with PREFIX.  */
  854.  
  855. static void
  856. print_variable (v, prefix)
  857.      register struct variable *v;
  858.      char *prefix;
  859. {
  860.   char *origin;
  861.  
  862.   switch (v->origin)
  863.     {
  864.     case o_default:
  865.       origin = "default";
  866.       break;
  867.     case o_env:
  868.       origin = "environment";
  869.       break;
  870.     case o_file:
  871.       origin = "makefile";
  872.       break;
  873.     case o_env_override:
  874.       origin = "environment under -e";
  875.       break;
  876.     case o_command:
  877.       origin = "command line";
  878.       break;
  879.     case o_override:
  880.       origin = "`override' directive";
  881.       break;
  882.     case o_automatic:
  883.       origin = "automatic";
  884.       break;
  885.     case o_invalid:
  886.     default:
  887.       abort ();
  888.       break;
  889.     }
  890.   printf ("# %s\n", origin);
  891.  
  892.   fputs (prefix, stdout);
  893.  
  894.   /* Is this a `define'?  */
  895.   if (v->recursive && index (v->value, '\n') != 0)
  896.     printf ("define %s\n%s\nendef\n", v->name, v->value);
  897.   else
  898.     {
  899.       register char *p;
  900.  
  901.       printf ("%s %s= ", v->name, v->recursive ? "" : ":");
  902.  
  903.       /* Check if the value is just whitespace.  */
  904.       p = next_token (v->value);
  905.       if (p != v->value && *p == '\0')
  906.     /* All whitespace.  */
  907.     printf ("$(subst ,,%s)", v->value);
  908.       else if (v->recursive)
  909.     fputs (v->value, stdout);
  910.       else
  911.     /* Double up dollar signs.  */
  912.     for (p = v->value; *p != '\0'; ++p)
  913.       {
  914.         if (*p == '$')
  915.           putchar ('$');
  916.         putchar (*p);
  917.       }
  918.       putchar ('\n');
  919.     }
  920. }
  921.  
  922.  
  923. /* Print all the variables in SET.  PREFIX is printed before
  924.    the actual variable definitions (everything else is comments).  */
  925.  
  926. static void
  927. print_variable_set (set, prefix)
  928.      register struct variable_set *set;
  929.      char *prefix;
  930. {
  931.   register unsigned int i, nvariables, per_bucket;
  932.   register struct variable *v;
  933.  
  934.   per_bucket = nvariables = 0;
  935.   for (i = 0; i < set->buckets; ++i)
  936.     {
  937.       register unsigned int this_bucket = 0;
  938.  
  939.       for (v = set->table[i]; v != 0; v = v->next)
  940.     {
  941.       ++this_bucket;
  942.       print_variable (v, prefix);
  943.     }
  944.  
  945.       nvariables += this_bucket;
  946.       if (this_bucket > per_bucket)
  947.     per_bucket = this_bucket;
  948.     }
  949.  
  950.   if (nvariables == 0)
  951.     puts ("# No variables.");
  952.   else
  953.     {
  954.       printf ("# %u variables in %u hash buckets.\n",
  955.           nvariables, set->buckets);
  956. #ifndef    NO_FLOAT
  957.       printf ("# average of %.1f variables per bucket, \
  958. max %u in one bucket.\n",
  959.           (double) nvariables / (double) set->buckets,
  960.           per_bucket);
  961. #else
  962.       {
  963.     int f = (nvariables * 1000 + 5) / set->buckets;
  964.     printf ("# average of %d.%d variables per bucket, \
  965. max %u in one bucket.\n",
  966.           f/10, f%10,
  967.           per_bucket);
  968.       }
  969. #endif
  970.     }
  971. }
  972.  
  973.  
  974. /* Print the data base of variables.  */
  975.  
  976. void
  977. print_variable_data_base ()
  978. {
  979.   puts ("\n# Variables\n");
  980.  
  981.   print_variable_set (&global_variable_set, "");
  982. }
  983.  
  984.  
  985. /* Print all the local variables of FILE.  */
  986.  
  987. void
  988. print_file_variables (file)
  989.      struct file *file;
  990. {
  991.   if (file->variables != 0)
  992.     print_variable_set (file->variables->set, "# ");
  993. }
  994.  
  995. #ifdef WINDOWS32
  996. void
  997. sync_Path_environment(void)
  998. {
  999.     char* path = allocated_variable_expand("$(Path)");
  1000.     static char* environ_path = NULL;
  1001.  
  1002.     if (!path)
  1003.         return;
  1004.  
  1005.     /*
  1006.      * If done this before, don't leak memory unnecessarily.
  1007.      * Free the previous entry before allocating new one.
  1008.      */
  1009.     if (environ_path)
  1010.         free(environ_path);
  1011.  
  1012.     /*
  1013.      * Create something WINDOWS32 world can grok
  1014.      */
  1015.     convert_Path_to_windows32(path, ';');
  1016.     environ_path = concat("Path", "=", path);
  1017.     putenv(environ_path);
  1018.     free(path);
  1019. }
  1020. #endif
  1021.